package com.example.myui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.myui.R;
import com.example.myui.model.ApkModel;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okserver.OkDownload;
import com.lzy.okserver.download.DownloadListener;

import java.io.File;
import java.text.NumberFormat;
import java.util.ArrayList;

public class MultiDownloadAdapter extends BaseQuickAdapter<ApkModel, BaseViewHolder> {
    private final NumberFormat numberFormat;
    private Context mContext;

    public MultiDownloadAdapter(Context context, ArrayList apks) {
        super(R.layout.item_download, apks);
        this.mContext = context;
        numberFormat = NumberFormat.getPercentInstance();
        numberFormat.setMinimumFractionDigits(2);
    }

    @Override
    protected void convert(BaseViewHolder holder, ApkModel apk) {
        ImageView apkicon = holder.getView(R.id.iv_apkicon);
        Glide.with(mContext)
                .load(apk.iconUrl)
                .into(apkicon);
        holder.setText(R.id.tv_apkname, apk.name);
        holder.getView(R.id.btn_download).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里只是演示，表示请求可以传参，怎么传都行，和okgo使用方法一样
                GetRequest<File> request = OkGo.<File>get(apk.url)//
                        .headers("aaa", "111")//
                        .params("bbb", "222");

                //这里第一个参数是tag，代表下载任务的唯一标识，传任意字符串都行，需要保证唯一,我这里用url作为了tag
                OkDownload.request(apk.url, request)//
                        .priority(apk.priority)//
                        .extra1(apk)//
                        .save()//
                        .register(new LogDownloadListener("LogDownloadListener", holder.getView(R.id.tv_progress)))//
                        .start();
                Log.i(TAG, "onClick: " + apk.url);
            }
        });
    }

    private class LogDownloadListener extends DownloadListener {
        private TextView progressview;

        public <T extends View> LogDownloadListener(String logDownloadListener, T view) {
            super(logDownloadListener);
            this.progressview = (TextView) view;
        }

        @Override
        public void onStart(Progress progress) {
            System.out.println("onStart: " + progress);
        }

        @Override
        public void onProgress(Progress progress) {
            System.out.println("onProgress: " + progress);
            progressview.setText(numberFormat.format(progress.fraction));
        }

        @Override
        public void onError(Progress progress) {
            System.out.println("onError: " + progress);
            progress.exception.printStackTrace();
        }

        @Override
        public void onFinish(File file, Progress progress) {
            System.out.println("onFinish: " + progress);
        }

        @Override
        public void onRemove(Progress progress) {
            System.out.println("onRemove: " + progress);
        }
    }
}
